<?php
/**
 * 考勤报告
 */
apf_require_controller('Oa_Attendance_Abstract');
apf_require_class("Tools_Tools");
apf_require_class("Bll_Attendance_Role");
apf_require_class("Bll_Login_User");
class Oa_Attendance_ReportExpController extends Oa_Attendance_AbstractController {
    const PAGEROW = 10;
    const MONTHWORKDAILY = 28;//当月工作结算日
    public function handle_request() {
        $Tools = Tools_Tools::get_instance();
        $params = $this->request->get_parameters();

        //获得需要显示的年、月
        $default_year = $this->get_default_year($params['select_year']);
        $params['select_year'] = $default_year;
        $default_month = $this->get_default_month($params['select_month']);
        $params['select_month'] = $default_month;
        $default_month_display = $params['select_year'].$params['select_month'].'01';
        $this->request->set_attribute('display_month',date('m月',strtotime($default_month_display)));

        //获取公司年份信息<下拉菜单>
        $company_year = Tools_Tools::company_year();
        $this->request->set_attribute('select_year',$company_year);

        //获取月份<下拉菜单>
        $company_month = array('01','02','03','04','05','06','07','08','09','10','11','12');
        $this->request->set_attribute('select_month',$company_month);

        //获取下属的部门和城市信息
        $search_department_city = $this->get_department_info($params['level']);
        //$this->request->set_attribute('select_department',$search_department_city['department']);
        $this->request->set_attribute('select_city',$search_department_city['cityname']);
        $this->request->set_attribute('citydepartment',$search_department_city['citydepartment']);


        //-----------------对hr做特殊处理
        $user_bll = new Bll_Login_User;
        $role_id = $user_bll->get_user_role_id($_SESSION['code']);
        if($role_id == 3){
            $search_department_city = $this->get_department_info_hr();
            //$this->request->set_attribute('select_department',$search_department_city['department']);
            $this->request->set_attribute('select_city',$search_department_city['cityname']);
            $this->request->set_attribute('citydepartment',$search_department_city['citydepartment']);
        }
        //-----------------对hr做特殊处理

        $base_domain = APF::get_instance()->get_config('base_domain');
        $this->request->set_attribute('base_domain',$base_domain.'/report/');

        //每月应工作的天数
        $work_att_day = $Tools->current_month_work_day($default_year,$default_month);
        $this->request->set_attribute('work_att_day',$work_att_day);

        //查询用户
        $userlist = $this->search_users($params,$work_att_day);
        $this->request->set_attribute('userlist',$userlist['data']);

        //分页
        if(!empty($userlist['total'])) {
            $url_params = array();//分页url参数
            if(!empty($params['level'])) {
                $url_params['level'] = $params['level'];
            }
            if(!empty($params['select_level'])) {
                $url_params['select_level'] = $params['select_level'];
            }
            if(!empty($params['select_city'])) {
                $url_params['select_city'] = $params['select_city'];
            }
            if(!empty($params['select_department'])) {
                $url_params['select_department'] = $params['select_department'];
            }
            if(!empty($params['keywords'])) {
                $url_params['keywords'] = $params['keywords'];
            }
            //$page_num = $params['p'];
            //$multipage = $this->build_multiPage_info($userlist['total'],$page_num,$url_params);
            //$this->request->set_attribute('multipage',$multipage);
        }
        //var_dump($userlist);
       /* $name = date("Y-m-d-H-i-s");
        //内容的类型
        header("Content-Type:application/vnd.ms-excel");
       // 以附件形式保存  $name 默认保存时的文件名
        header("Content-Disposition:attachment;filename=$name.csv");
       // 不缓存
        header("Pragma:no-cache");
        // 浏览器不缓存的时间
        header("Expires:0");
        echo "\xEF\xBB\xBF员工,工号,部门,实际,应出勤率,餐费,迟到,公出(小时),出差,事假(天),病假,年假,其他假"."\n";
        foreach ($userlist['data'] as $v){
            echo $v['name'].",".$v['code'].",".$v['department_name'].",".$v['work_act_day'].",".$v['rice_day'].",".$v['meal'].",".
            $v['oa_cd'].",".$v['oa_gc'].",".$v['oa_cc'].",".$v['oa_sj'].",".$v['oa_bj'].",".$v['oa_nj'].",".$v['oa_oj']."\n";
        }*/
        $this->export_xls($userlist['data'],$work_att_day);
        exit();
    }

    public function export_xls($data,$work_att_day){
        if(empty($data)) {
            return '';
        }
        $filename = '考勤明细_'.date('YmdHis',time()).'.xls';
        $dl_index = '<tr height="30">';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">员工</th>';
        $dl_index .= '<th width="60" bgColor="#CCCCCC" align="center">工号</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">部门</th>';
        $dl_index .= '<th width="50" bgColor="#CCCCCC" align="center">实际</th>';
        $dl_index .= '<th width="80" bgColor="#CCCCCC" align="center">应出勤率</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">餐费</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">迟到</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">旷工</th>';
        $dl_index .= '<th width="90" bgColor="#CCCCCC" align="center">公出(小时)</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">出差</th>';
        $dl_index .= '<th width="70" bgColor="#CCCCCC" align="center">事假(天)</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">病假</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">年假</th>';
        $dl_index .= '<th width="100" bgColor="#CCCCCC" align="center">其他假</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">上月未清</th>';
        $dl_index .= '<th width="150" bgColor="#CCCCCC" align="center">未全勤</th>';
        $dl_index .= '</tr>';
        foreach ($data as $key => $v) {
            $oa_prexj = '';
            if(!empty($v['oa_prexj'])){
                foreach($v['oa_prexj'] as $k=>$value){
                    $oa_prexj .= $value."天".$k;
                }
            }
            if(empty($oa_prexj)){
                $oa_prexj = 0;
            }
            //防止出现excel中出现1/1自动换成1月1日
            $v['oa_gc'] = $v['oa_gc'] > 0 ? number_format($v['oa_gc'],1) : 0;
            $v['oa_gc_passed'] = $v['oa_gc_passed'] > 0 ? number_format($v['oa_gc_passed'],1) : 0;
            $v['oa_cc'] = $v['oa_cc'] > 0 ? number_format($v['oa_cc'],1) : 0;
            $v['oa_cc_passed'] = $v['oa_cc_passed'] > 0 ? number_format($v['oa_cc_passed'],1) : 0;
            $v['oa_sj'] = $v['oa_sj'] > 0 ? number_format($v['oa_sj'],1) : 0;
            $v['oa_sj_passed'] = $v['oa_sj_passed'] > 0 ? number_format($v['oa_sj_passed'],1) : 0;
            $v['oa_bj'] = $v['oa_bj'] > 0 ? number_format($v['oa_bj'],1) : 0;
            $v['oa_bj_passed'] = $v['oa_bj_passed'] > 0 ? number_format($v['oa_bj_passed'],1) : 0;
            $v['oa_nj'] = $v['oa_nj'] > 0 ? number_format($v['oa_nj'],1) : 0;
            $v['oa_nj_passed'] = $v['oa_nj_passed'] > 0 ? number_format($v['oa_nj_passed'],1) : 0;
            $v['oa_oj'] = $v['oa_oj'] > 0 ? number_format($v['oa_oj'],1) : 0;
            $v['oa_oj_passed'] = $v['oa_oj_passed'] > 0 ? number_format($v['oa_oj_passed'],1) : 0;

            $dl_list .= '<tr height="26">';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['code'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['department_name'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['work_act_day'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$work_att_day.'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['meal'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['oa_cd'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['oa_kg'].'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_gc']) ? 0 : $v['oa_gc']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_cc']) ? 0 : $v['oa_cc']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_sj']) ? 0 : $v['oa_sj']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_bj']) ? 0 : $v['oa_bj']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_nj']) ? 0 : $v['oa_nj']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.(empty($v['oa_oj']) ? 0 : $v['oa_oj']).'</td>';
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$oa_prexj.'</td>';//..................
            $dl_list .= '<td bgColor="#FFFFFF" align="center">'.$v['work_act_not_day'].'</td>';
            $dl_list .= '</tr>';
        }
        $dl_data = $dl_index.$dl_list;
$xls = <<<EOT
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
</head>
<body>
<table border="1" width="900">
$dl_data
</table>
</body>
</html>
EOT;
        ob_clean();
        $filename = strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !==false ? urlencode($filename) : $filename;
        header("Content-type:   application/vnd.ms-excel");
        header("Content-Transfer-Encoding: binary");
        header("Accept-Ranges:   bytes");
        header("Accept-Length:   ".strlen($xls));
        header("Content-Disposition:   attachment;   filename=" . $filename);
        echo $xls;
        exit;
    }

    /**
     * 获得本月结束日
     */
    public function get_month_word_end_day($yearmonth = 0) {
        $Tools = Tools_Tools::get_instance();
        if(empty($yearmonth)) {
            $yearmonth = date('Ym');
        }
        $workendday = $yearmonth.self::MONTHWORKDAILY;
        //查找数据库里是否有当月的结算时间
        $bll_monthset = $this->init_month_set();
        $month_set = $bll_monthset->get_month_special_set($yearmonth);
        if(!empty($month_set['settlement'])) {
            $workendday = substr($month_set['settlement'],0,10);
        }
        return $workendday;
    }

    /**
     * 一个月的实际工作日
     * 1.优先从表中查询是否有设置月份的结束工作日。
     * @param unknown_type $workendday
     */
    public function get_month_work_day($yearmonth = 0) {
        $Tools = Tools_Tools::get_instance();
        $workday = 0;
        if(empty($yearmonth)) {
            $yearmonth = date('Ym');
        }
        $yearmonthday = $yearmonth.self::MONTHWORKDAILY;
        //查找数据库里是否有当月的结算时间
        $bll_monthset = $this->init_month_set();
        $month_set = $bll_monthset->get_month_special_set($yearmonth);
        if(empty($month_set)) {
            $time = strtotime($yearmonthday);
            $workday = $Tools->dissmion_work_day_by_month($time);
        }else {
            $workday = $month_set['work_day'];
            if(empty($workday) && !empty($month_set['settlement'])) {
                $time = substr($month_set['settlement'],0,10);
                $time = strtotime($time);
                $workday = $Tools->dissmion_work_day_by_month($time);
            }else if(empty($workday)) {
                $time = strtotime($yearmonthday);
                $workday = $Tools->dissmion_work_day_by_month($time);
            }
        }
        return $workday+1;
    }

    /**
     * 得到年份
     */
    public function get_default_year($year) {
        $year = (int)$year;
        if(empty($year)) {
            $year = date('Y');
        }
        return $year;
    }

    /**
     * 得到月份
     */
    public function get_default_month($month) {
        if(empty($month)) {
            $month = date('m');
        }
        return $month;
    }

      /**
       * 搜索用户
       * @param unknown_type $params
       * @param int $work_att_day 每月应工作多少天
       */
    public function search_users($params,$work_att_day) {
        apf_require_controller("Oa_Attendance_UserDetail");
        $userdetail_controller = new Oa_Attendance_UserDetailController();
        $where = array();
        $where['superior_id'] = $this->uinfo['user_id'];
        //$where['superior_id'] = 1;
        if(!empty($params['level'])) {
            $where['level'] = $params['level'];
        }
        if(!empty($params['select_level'])) {
            $where['level'] = $params['select_level'];
        }
        if(!empty($params['select_city'])) {
            $where['city_name'] = $params['select_city'];
        }
        if(!empty($params['select_department'])) {
            $where['department_id'] = $params['select_department'];
        }
        if(!empty($params['keywords'])) {
            $where['name'] = $params['keywords'];
        }

        //传递到_find_short_assoc
        if(!empty($params['select_year'])){
            $where['select_year'] = $params['select_year'];
        }
        if(!empty($params['select_month'])){
            $where['select_month'] = $params['select_month'];
        }

        $bll_user = $this->init_user();
        $bll_user->enable_cache = false;
        $bll_userinfo = $this->init_userinfo();

        //-----------------对hr做特殊处理
        $user_bll = new Bll_Login_User;
        $role_id = $user_bll->get_user_role_id($_SESSION['code']);
        if($role_id == 3){
            //除上海外，其他得到HR只能获取该城市的用户
            unset($where['level']);
            //$result = $bll_user->search_subordinate_userinfo_hr($where,$order,self::PAGEROW,$offset);
            $result = $bll_user->search_subordinate_exp_userinfo_hr($where);
        }
        else{
            //$result = $bll_user->search_subordinate_userinfo($where,$order,self::PAGEROW,$offset);
            $result = $bll_user->search_subordinate_exp_userinfo($where);
        }
        //----------------对hr做特殊处理

        //组装用户的考勤信息
        if(!empty($result['data'])) {
            apf_require_class("Bll_Attendance_Report");
            $report = new Bll_Attendance_Report();
            $report->set_data_type(0);
            $user_ids = array();
            foreach($result['data'] as $key=>$v) {
                if($params['select_year'].$params['select_month'] < date('Ym',strtotime($v['enter_date']))){
                    unset($result['data'][$key]);
                    continue;
                }
                $report_data = $report->build_report_execl_data($v['user_id'],$where);
                $result['data'][$key] = array_merge($result['data'][$key],$report_data);
            }
            return $result;

/*******************************************开始用了新的方法************************************************/

            $user_ids = array();
            foreach($result['data'] as $v) {
                $user_ids[] = $v['user_id'];
            }
            $time = $this->get_time($params['select_year'].$params['select_month'].'01');
            //用户的考勤记录
            $record_info = $bll_userinfo->get_users_record($user_ids,$time['start'],$time['end']);

            //获得用户的上月未扣数据<只获取用户的请假信息>
            $prex_record_info = $this->get_prex_record_info($user_ids,$time['start']);
            //员工应扣餐补天数
            $leave_info_all = $this->memger_leavetime($record_info['leave_info_all']);
            //用户请假信息
            $leave_info = $this->memger_leavetime($record_info['leave_info']);
            //用户迟到信息
            $belate_info = $this->memger_belateinfo($record_info['belate_info']);
            //用户公出信息
            $corptrip_info = $this->memger_corptripinfo($record_info['corptrip_info']);
            //用户出差信息
            $trip_info = $this->memger_tripinfo($record_info['trip_info']);
            //用户事假信息
            $sj_info = $this->memger_sjinfo($record_info['sj_info']);
            //用户病假信息
            $bj_info = $this->memger_bjinfo($record_info['bj_info']);
            //用户年假信息
            $nj_info = $this->memger_njinfo($record_info['nj_info']);
            //用户其他假信息
            $oj_info = $this->memger_ojinfo($record_info['oj_info']);

            $Ym = date('Ym');
            $_Ym = $params['select_year'].$params['select_month'];

            //组装
            foreach($result['data'] as $key=>$v) {
                //实际工作天数
                $work_act_day = 0;
                if($_Ym<=$Ym) {
                    $work_act_day = $work_att_day;
                    $work_meal_day = $work_att_day;
                }

                $oa_cd = 0;//迟到
                $oa_gc = 0;//公出
                $oa_cc = 0;//出差
                $oa_sj = 0;//事假
                $oa_bj = 0;//病假
                $oa_nj = 0;//年假
                $oa_oj = 0;//其他假
                $oa_prexj = 0;//上月未扣

                $oa_kg = 0;//旷工天数


                //start----------特殊处理
                apf_require_class('Tools_Tools');
                $Tools = Tools_Tools::get_instance();
                $start_year = $params['select_year'];
                $start_month = $params['select_month'];
                //当月入职工作天数统计
                $entertime = strtotime($v['enter_date']);
                $firstday = mktime(0,0,0,$start_month,1,$start_year);
                $Dimission = strtotime($v['dimission_date']);
                $flag_dis_enter = false;
                $flag_dismiss = false;
                $flag_enter = false;

                //当月入职工作天数统计
                if($entertime > $firstday) {
                    if(date('Y-m',$entertime) == date('Y-m',mktime(0,0,0,$start_month,1,$start_year))) {
                        $work_act_enter_day =  $Tools->dissmion_work_day_by_month($entertime);//本月月初有多少天没上班
                    } else {
                        $work_act_enter_day = 0;
                    }
                    $flag_enter = true;
                }
                //当月离职工作天数统计
                if($v['dimission_date'] != '0000-00-00 00:00:00') {
                    if(date('Ym',$Dimission) == date('Ym',mktime(0,0,0,$start_month,1,$start_year))) {
                        //当前月离职
                        $work_act_day_dismiss = $Tools->dissmion_work_day_by_month($Dimission)+1;
                    } elseif(date('Ym',$Dimission) < date('Ym',mktime(0,0,0,$start_month,1,$start_year))) {
                        //离职小于当前时间
                        $work_act_day_dismiss = 0;
                    }
                    $flag_dismiss = true;
                }
                //当月入职,当月离职
                if (date('Ym',$Dimission) == date('Ym',$entertime)) {
                    $work_act_day_dismiss = $Tools->dissmion_work_day_by_month($Dimission) - $Tools->dissmion_work_day_by_month($entertime)+1;
                    $flag_dis_enter = true;
                }
                //----------------end

                //领餐补天数
                if(isset($leave_info_all[$v['user_id']]) or $flag_dis_enter or $flag_dismiss or $flag_enter) {
                    $work_meal_day = $work_att_day-$leave_info_all[$v['user_id']]['leave_time'];
                    //本月离职的
                    if($flag_dismiss or $flag_dis_enter){
                        $work_meal_day = $work_act_day_dismiss-$leave_info_all[$v['user_id']]['leave_time'];
                    }
                    //本月入职
                    elseif($flag_enter){
                        $work_meal_day = $work_meal_day - $work_act_enter_day;
                    }
                }
                //领工资天数
                if(isset($leave_info[$v['user_id']]) or $flag_dis_enter or $flag_dismiss or $flag_enter) {
                    $work_act_day = $work_att_day-$leave_info[$v['user_id']]['leave_time'];
                    //本月离职的
                    if($flag_dismiss or $flag_dis_enter){
                        $work_act_day = $work_act_day_dismiss-$leave_info[$v['user_id']]['leave_time'];
                    }
                    //本月入职
                    elseif($flag_enter){
                        $work_act_day = $work_act_day - $work_act_enter_day;
                    }
                }
                if(isset($belate_info[$v['user_id']])) {
                    $oa_cd = $belate_info[$v['user_id']]['oa_cd'];
                }
                if(isset($corptrip_info[$v['user_id']])) {
                    $oa_gc = $corptrip_info[$v['user_id']]['leave_time'];
                }
                if(isset($trip_info[$v['user_id']])) {
                    $oa_cc = $trip_info[$v['user_id']]['leave_time'];
                }
                if(isset($sj_info[$v['user_id']])) {
                    $oa_sj = $sj_info[$v['user_id']]['leave_time'];
                }
                if(isset($bj_info[$v['user_id']])) {
                    $oa_bj = $bj_info[$v['user_id']]['leave_time'];
                }
                if(isset($nj_info[$v['user_id']])) {
                    $oa_nj = $nj_info[$v['user_id']]['leave_time'];
                }
                if(isset($oj_info[$v['user_id']])) {
                    $oa_oj = $oj_info[$v['user_id']]['leave_time'];
                }
                if(isset($prex_record_info[$v['user_id']])) {
                    $oa_prexj = $prex_record_info[$v['user_id']]['leave_time'];
                }

                //迟到旷工
                $my = array('uid'=>$v['user_id'],'ym'=>$_Ym);
                $cd_kg = $bll_userinfo->get_cd_kg($my);
                $oa_cd = $cd_kg['cd']?$cd_kg['cd']:0;
                $oa_kg = $cd_kg['kg']?$cd_kg['kg']:0;

                $result['data'][$key]['work_act_day'] = $work_act_day;
                $result['data'][$key]['rice_day'] = floor($work_meal_day);
                //餐费信息
                $result['data'][$key]['meal'] = $this->get_meal($work_att_day,$work_meal_day);
                $result['data'][$key]['oa_cd'] = $oa_cd;
                $result['data'][$key]['oa_gc'] = $oa_gc;
                $result['data'][$key]['oa_cc'] = $oa_cc;
                $result['data'][$key]['oa_sj'] = $oa_sj;
                $result['data'][$key]['oa_bj'] = $oa_bj;
                $result['data'][$key]['oa_nj'] = $oa_nj;
                $result['data'][$key]['oa_oj'] = $oa_oj;
                $result['data'][$key]['oa_kg'] = $oa_kg;//旷工
                $result['data'][$key]['oa_prexj'] = $oa_prexj;
                $result['data'][$key]['oa_url'] = $userdetail_controller->build_url($v['user_id'],$params['select_year'].$params['select_month']);
                $result['data'][$key]['work_act_not_day'] = $Tools->current_month_not_work_day($params['select_year'],$params['select_month'],date('Ymd',strtotime($v['enter_date'])),date('Ymd',strtotime($v['dimission_date'])));
            }
        }
        return $result;
    }

    /**
     * 获得上月未扣的假
     * @param unknown_type $uids
     * @param unknown_type $endtime
     */
    public function get_prex_record_info($uids,$endtime = 0) {
        $time = strtotime($endtime);
        $month = date('m',$time);
        //1.得到上月的结算时间
        $start = date('Ym',mktime(0,0,0,$month-1,1,date('Y-m-d',$time)));
        $start = $this->get_month_word_end_day($start);
        $time = strtotime($start)+86400;
        $start = date('Y-m-d',$time);
        $end = date('Y-m-d',mktime(0,0,0,$month,1,date('Y',$time))-86400);

        $bll_userinfo = $this->init_userinfo($uids,$start,$end);
        $result = $bll_userinfo->get_users_record($uids,$start,$end);
        return $this->memger_leavetime($result['leave_info']);
    }

    /**
     * 餐补计算
     * @param unknown_type $att_day 应出勤天数
     * @param unknown_type $act_day 实出勤天数
     */
    public function get_meal($att_day,$act_day) {
        $meal = APF::get_instance()->get_config('oa_meal');
        $meal = round($meal/$att_day*$act_day);
        return $meal;
    }

    /**
     * 时间格式处理
     */
    public function get_time($time = 0) {
        $result = array('start'=>0,'end'=>'0');
        if(empty($time)) {
            return $result;
        }
        //echo $time;
        $time = strtotime($time);
        $month = date('m',$time);
        $start = date('Y-m-d',mktime(0,0,0,$month,1,date('Y',$time)));//本月开始
        $end = date('Y-m-d',mktime(0,0,0,$month+1,1,date('Y',$time)));//结束日期
        $end = $this->get_month_word_end_day(date('Y-m-',$time));
        $result['start'] = $start;
        $result['end'] = $end." 23";
        return $result;
    }

    /**
     * 获取当前用户下属的部门信息
     */
    public function get_department_info($level) {
        //得到当前用户的下属列表,1级下属
        $user_id = $this->uinfo['user_id'];
        $bll_role = new Bll_Attendance_Role();
        if(empty($level)) {$level=1;}else if($level=='all') {$level=0;}
        $subordinate_users = $bll_role->get_subordinate_by_user(1,$level);
        if(empty($subordinate_users)) {
            return array();
        }

        $user_ids = array();
        foreach($subordinate_users as $v) {
            $user_ids[] = $v['user_id'];
        }

        //得到用户部门信息
        $bll_user = new Bll_Login_User();
        $user_info = $bll_user->get_user_info_by_ids($user_ids);
        //$department_info = array();
        $city_info = array();
        $citydepartment = array();//城市与部门的对应关系
        $data = array();
        if(!empty($user_info)) {
            foreach($user_info as $v) {
                $tmp = array();
                $tmp['department_id'] = $v['department_id'];
                $tmp['department_name'] = $v['department_name'];

                //$department_info[$v['department_id']] = $v['department_name'];
                $city_info[] = $v['city_name'];
                $citydepartment[$v['city_name']][$v['department_id']] = $tmp;
            }
            if(!empty($citydepartment)) {
                foreach($citydepartment as $cityname => $v) {
                    ksort($v);
                    $v = array_values($v);
                    $citydepartment[$cityname] = $v;
                }
            }
        }
        $city_info = array_unique($city_info);
        //$data['department'] = $department_info;
        $data['cityname'] = $city_info;
        $data['citydepartment'] = json_encode($citydepartment);
        return $data;
    }

    /**
     * 获取翻页
     * @param unknown_type $totalnum 总的页数
     * @param unknown_type $page_num 当前第几页
     * @param array $url_params
     * @param int $uid 用户ID
     */
    public function build_multiPage_info($totalnum,$page_num = 0,$url_params = array()) {
        apf_require_class('Tools_MultiPage');
        //翻页信息
        $url = "/report/";
        $total_rows = $totalnum;
        if($page_num <= 0){$page_num = 1;}
        $url_params['p'] = $page_num;

        $multipage = Tools_MultiPage::multiPage($url,$url_params, '', $total_rows, self::PAGEROW, 10);
        return $multipage;
    }

    /**
     * 组合用户其他假信息
     */
    public function memger_ojinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户年假信息
     */
    public function memger_njinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户病假信息
     */
    public function memger_bjinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户事假信息
     * @param unknown_type $rs
     */
    public function memger_sjinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户出差信息
     * @param unknown_type $rs
     * @return multitype:|Ambigous <multitype:, unknown>
     */
    public function memger_tripinfo($rs) {
        $result = $this->memger_leavetime($rs);
        return $result;
    }

    /**
     * 组合用户公出信息
     * @param unknown_type $rs
     * @return array(userid=>公出天数)
     */
    public function memger_corptripinfo($rs) {
        $result = $this->memger_leavetime($rs);
//         if(!empty($result)) {
//             foreach($result as $uid => $info) {
//                 $result[$uid]['leave_time'] =  sprintf("%.1f",$result[$uid]['leave_time']/8);
//             }
//         }
        return $result;
    }

    /**
     * 组合用户迟到信息
     * @param unknown_type $rs
     * @return array(userid=>迟到次数)
     */
    public function memger_belateinfo($rs) {
        $result = array();
        if(empty($rs)) {return array();}
        foreach($rs as $key=>$v) {
            if(isset($result[$v['user_id']])) {
                $result[$v['user_id']]['oa_cd'] += 1;
            }else {
                $result[$v['user_id']]['oa_cd'] = 1;
            }
        }
        return $result;
    }

    /**
     * 组合用户请假信息
     * @param unknown_type $rs
     * @return array(userid=>请假天数)
     */
    public function memger_leavetime($rs) {
        $result = array();
        if(empty($rs)) {return array();}
        foreach($rs as $v) {
            if(isset($result[$v['user_id']])) {
                $result[$v['user_id']]['leave_time'] += $v['leave_time'];
            }else {
                $result[$v['user_id']]['leave_time'] = $v['leave_time'];
            }
        }
        return $result;
    }

    /**
     * 获取当前用户下属的部门信息 HR
     */
    public function get_department_info_hr() {
        //所有用户信息
        $all_users = array();
        apf_require_class('Bll_Attendance_UserInfo');
        $bll_user = new Bll_Attendance_UserInfo;
        $all_users = $bll_user->get_all_users();

        $user_ids = array();
        foreach($all_users as $v) {
            $user_ids[] = $v['user_id'];
        }

        //得到用户部门信息
        $bll_user = new Bll_Login_User();
        $user_info = $bll_user->get_user_info_by_ids($user_ids);
        //$department_info = array();
        $city_info = array();
        $citydepartment = array();//城市与部门的对应关系
        $data = array();
        if(!empty($user_info)) {
            foreach($user_info as $v) {
                $tmp = array();
                $tmp['department_id'] = $v['department_id'];
                $tmp['department_name'] = $v['department_name'];

                //$department_info[$v['department_id']] = $v['department_name'];
                $city_info[] = $v['city_name'];
                $citydepartment[$v['city_name']][$v['department_id']] = $tmp;
            }
            if(!empty($citydepartment)) {
                foreach($citydepartment as $cityname => $v) {
                    ksort($v);
                    $v = array_values($v);
                    $citydepartment[$cityname] = $v;
                }
            }
        }
        $city_info = array_unique($city_info);
        //$data['department'] = $department_info;
        $data['cityname'] = $city_info;
        $data['citydepartment'] = json_encode($citydepartment);
        return $data;
    }
}